<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="loginUser.userAccount"
          name="userAccount"
          label="账号"
          placeholder="账号"
          :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
          v-model="loginUser.userPassword"
          type="password"
          name="userPassword"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>

</template>

<script setup lang="ts">
import {ref} from 'vue'
import myAxios from "../plugins/myAxios.js";
import {showFailToast, showSuccessToast} from "vant";
import {useRouter} from "vue-router";

const router = useRouter();

const initUser={
  userAccount:'',
  userPassword:''
}

const loginUser=ref({...initUser})


const onSubmit = async () => {
  const userLoginData={
    ...loginUser.value
  }
  //请求后端登录用户登录接口
 const res=await myAxios.post('/user/login',userLoginData)
  if (res.code===0 && res.data){
    showSuccessToast('登录成功!')
    //登录这个之后 回到主页 back是覆盖页面 返回到覆盖前的页面 用back就返回到了登录页 replace是代替之前的页面 不会出现类似情况
    await router.replace('/')
  }else {
    showFailToast('登录失败!')
  }
};

</script>

<style scoped>

</style>



